package com.dhz.common.id.redis;

import com.dhz.common.redis.RedisServerConf;
import org.apache.commons.io.IOUtils;
import redis.clients.jedis.Jedis;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author FY
 */
public class IdServerConf extends RedisServerConf {
    @Override
    public void loadLuaScript() throws IOException {
        Jedis jedis = this.getPool().getResource();

        InputStream is = null;
        try {
            if (null != getLuaFilePath() && !"".equals(getLuaFilePath())) {
                is = new FileInputStream(new File(getClass().getResource("/").getPath() + this.getLuaFilePath()));
            } else {
                is = getClass().getResourceAsStream("/luascript/idgenerator2.lua");
            }
            this.setLuaSha(jedis.scriptLoad(IOUtils.toString(is, "UTF-8")));
        } finally {
            if (is != null) {
                is.close();
            }
            this.getPool().returnResourceObject(jedis);
        }
    }
}
